﻿using DBOptionPro.DAL;
using DBOptionPro.Model;
using DevExpress.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBOptionPro.BLL
{
    /// <summary>
    /// 同步业务逻辑处理类
    /// </summary>
    public class SyncBLL
    {
        /// <summary>
        /// 构造
        /// </summary>
        public SyncBLL()
        {
            _SysOptionBLL = new SysOptionBLL();
            _AreaBLL = new AreaBLL();
        }

        /// <summary>
        /// 系统设置业务逻辑处理对象
        /// </summary>
        private readonly SysOptionBLL _SysOptionBLL;
        /// <summary>
        /// 地区信息业务逻辑处理对象
        /// </summary>
        private readonly AreaBLL _AreaBLL;

        /// <summary>
        /// 消息传递委托 
        /// </summary>
        public Action<string, int> ShowMessage;

        /// <summary>
        /// 停止同步标记
        /// </summary>
        private bool _StopFlag = false;

        /// <summary>
        /// 更新计数器
        /// </summary>
        public long UpdateCount { get; set; } = 0;
        /// <summary>
        /// 插入计数器
        /// </summary>
        public long InsertCount { get; set; } = 0;
        /// <summary>
        /// 同步(开新线程执行)
        /// </summary>
        /// <param name="file">文件信息对象</param>
        /// <returns></returns>
        public async Task<bool> Sync(DBFileInfo file)
        {
            _StopFlag = false;
            var option = _SysOptionBLL.GetOption();
            MyData_ServerDAL serverDAL = null;
            MyDataDAL dal = null;
            try
            {
                UpdateCount = 0;
                InsertCount = 0;
                var areas = _AreaBLL.GetAreas();
                serverDAL = new MyData_ServerDAL(option);
                dal = new MyDataDAL(file, option, areas);
                Dictionary<string, string[]> sourFields = serverDAL.GetSourFields(file.AreaCode);// 成绩字段
                var area = areas.Find(t => t.AreaCode == file.AreaCode);
                string info = $"获取{area.AreaName}{_SysOptionBLL.GetOption().Year}成绩字段数据失败！";
                if (sourFields == null)
                {
                    //throw new Exception(info);
                    ShowMessage?.Invoke(info, 3);
                    return false;
                }

                var datas = dal.GetMyDatas(sourFields);
                if (datas != null && datas.Count > 0)
                {
                    int temp = 0;
                    foreach (var item in datas)
                    {
                        if (_StopFlag) throw new Exception("强行终止！");
                        if (serverDAL.Exists(item.ksh))
                        {
                            await serverDAL.Update(item);
                            UpdateCount++;
                        }
                        else
                        {
                            await serverDAL.Insert(item);
                            InsertCount++;
                        }
                        Console.WriteLine(temp++);
                    }
                    serverDAL.UpdateSyncTable(file.AreaCode, DateTime.Now, "成功");

                }
                return true;
            }
            catch (Exception ex)
            {
                serverDAL?.UpdateSyncTable(file.AreaCode, DateTime.Now, "失败");
                dal?.Dispose();
                serverDAL?.Dispose();
                Console.WriteLine(ex);
                return false;
            }
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            _StopFlag = true;
        }
    }
}
